package com.pinyougou.shop.realm;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.Seller;
import com.pinyougou.service.SellerService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class SellerAuthorizingRealm extends AuthorizingRealm {

    @Reference(timeout = 10000)
    private SellerService sellerService;


    @Override//授权方法
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取登录名
        try {
            String username = (String) authenticationToken.getPrincipal();
            System.out.println("username="+username);
            System.out.println("sellerService="+sellerService);

            //根据用户名查询商家对象
            Seller seller = sellerService.findOne(username);

            //判单seller是否为空,审核状态码是否为1
            if (seller !=null &&"1".equals(seller.getStatus())){

                //返回身份认证对象
                return new SimpleAuthenticationInfo(username, seller.getPassword(),
                        ByteSource.Util.bytes(seller.getSellerId()), this.getName());
            }
        } catch (Exception e) {
            throw new AuthenticationException(e);
        }
        return null;

    }
}
